#include "VPathEditDialog.h"
#include "ui_VPathEditDialog.h"
#include "VListWidgetItemEditDialog.h"
#include <QFileDialog>
#include <QString>
#include <QUrl>
#include <QDir>
#include <QPushButton>
#include <QDebug>
#include <QRect>

VPathEditDialog::VPathEditDialog(QWidget *parent, QString source_path, QStringList list) :
    QDialog(parent),
    ui(new Ui::VPathEditDialog)
{
    ui->setupUi(this);
    _source_path = source_path;
    foreach(QString path, list)
    {
        if(path.trimmed().isEmpty()==false)
        {
            ui->list_widget_paths->addItem(path);
        }
    }
}

VPathEditDialog::~VPathEditDialog()
{
    delete ui;
}

void VPathEditDialog::on_list_widget_paths_doubleClicked(const QModelIndex &index)
{
    auto item = ui->list_widget_paths->currentItem();
    if(item != nullptr)
    {
        VListWidgetItemEditDialog dlg(this, _source_path, item->text(), _type);
        int result = dlg.exec();
        if(result == QDialog::Accepted)
        {
            item->setText(dlg._path);
        }
//        QRect rect = ui->list_widget_paths->visualItemRect(item);
//        _btn.move(ui->list_widget_paths->width()-_btn.width(), rect.top());
//        _btn.show();
//        QString text = item->text();
//        QString path = QFileDialog::getExistingDirectory(this, "选择路径", text);
//        if(path.isEmpty() == false)
//        {
//            QDir dir;


//            item->setText(dir.relativeFilePath(path));
//        }
    }

}

void VPathEditDialog::on_btn_new_clicked()
{
    VListWidgetItemEditDialog dlg(this, _source_path, "", _type);
    dlg._filter = this->_filter;
    int result = dlg.exec();
    if(result == QDialog::Accepted)
    {
        auto item = new QListWidgetItem(ui->list_widget_paths);
        item->setText(dlg._path);
    }
}

void VPathEditDialog::on_ben_del_clicked()
{
    auto item = ui->list_widget_paths->currentItem();
    if(item != nullptr)
    {
        delete item;
    }
}

void VPathEditDialog::on_btn_up_clicked()
{
    auto item = ui->list_widget_paths->currentItem();
    auto row = ui->list_widget_paths->currentRow();
    if(row > 0)
    {
        QString temp = ui->list_widget_paths->item(row-1)->text();
        ui->list_widget_paths->item(row-1)->setText(item->text());
        item->setText(temp);
        ui->list_widget_paths->setCurrentRow(row-1);
    }
}

void VPathEditDialog::on_btn_down_clicked()
{
    auto item = ui->list_widget_paths->currentItem();
    auto row = ui->list_widget_paths->currentRow();
    if(row < ui->list_widget_paths->count()-1)
    {
        QString temp = ui->list_widget_paths->item(row+1)->text();
        ui->list_widget_paths->item(row+1)->setText(item->text());
        item->setText(temp);
        ui->list_widget_paths->setCurrentRow(row+1);
    }
}

void VPathEditDialog::on_btn_ok_clicked()
{
    _list.clear();
    for(int i=0; i<ui->list_widget_paths->count(); i++)
    {
        QString text = ui->list_widget_paths->item(i)->text();
        if(text.trimmed().isEmpty() == false)
        {
            _list.append(text);
        }
    }
    this->accept();
}

void VPathEditDialog::on_btn_cancel_clicked()
{
    this->reject();
}
